{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2021-06-09 00:00:00')"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2021-6-9')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2012-01-01 00:00:00')"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数值型转时间戳\n",
    "pd.Timestamp(2012,1,1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "Timestamp('2021-01-01 01:01:01')"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 大时间只支持 - 连接 后面的小时间只能用冒号\n",
    "pd.Timestamp('2021-1-1 1:1:1')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03'], dtype='datetime64[ns]', freq=None)"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    ">>> df = pd.DataFrame({'year': [2015, 2016],\n",
    "...                    'month': [2, 3],\n",
    "...                    'day': [4, 5]})\n",
    ">>> pd.to_datetime(df)\n",
    "0   2015-02-04\n",
    "1   2016-03-05\n",
    "\n",
    "\n",
    "如果日期不符合时间戳限制 ，则传递 errors='ignore' 将返回原始输入而不是引发任何异常。\n",
    "除了强制将非日期（或不可解析的日期）强制为 NaT 之外，传递 errors='coerce' 将强制将越界日期强制为 NaT。\n",
    ">>> pd.to_datetime('13000101', format='%Y%m%d', errors='ignore')\n",
    "datetime.datetime(1300, 1, 1, 0, 0)\n",
    ">>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce')\n",
    "NaT\n",
    "传递 infer_datetime_format=True 通常可以加速解析，如果它不是完全 ISO8601 格式，而是常规格式。\n",
    ">>> s = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000'] * 1000)\n",
    ">>> s.head()\n",
    "0    3/11/2000\n",
    "1    3/12/2000\n",
    "2    3/13/2000\n",
    "3    3/11/2000\n",
    "4    3/12/2000\n",
    "dtype: object\n",
    "\n",
    ">>> %timeit pd.to_datetime(s, infer_datetime_format=True)  # doctest: +SKIP\n",
    "100 loops, best of 3: 10.4 ms per loop\n",
    ">>> %timeit pd.to_datetime(s, infer_datetime_format=False)  # doctest: +SKIP\n",
    "1 loop, best of 3: 471 ms per loop\n",
    "使用 Unix 纪元时间\n",
    ">>> pd.to_datetime(1490195805, unit='s')\n",
    "Timestamp('2017-03-22 15:16:45')\n",
    ">>> pd.to_datetime(1490195805433502912, unit='ns')\n",
    "Timestamp('2017-03-22 15:16:45.433502912')\n",
    "\n",
    "对于 float arg，可能会发生精确舍入。 为了防止意外行为，请使用固定宽度的精确类型。\n",
    "使用非 Unix 纪元起源\n",
    ">>> pd.to_datetime([1, 2, 3], unit='D',\n",
    "...                origin=pd.Timestamp('1960-01-01'))\n",
    "DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'],\n",
    "dtype='datetime64[ns]', freq=None)\n",
    "\"\"\"\n",
    "# 将参数转换为日期时间。\n",
    "pd.to_datetime([\"2021-1-1\",\"2021-1-2\",\"2021-1-3\"])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2017-07-01', '2017-10-10', 'NaT'], dtype='datetime64[ns]', freq=None)"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 还支持日期英文缩写\n",
    "pd.to_datetime(['Jul 1, 2021', '2021-10-10', None])\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2021-10-01', '2021-01-31'], dtype='datetime64[ns]', freq=None)"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(['2021/10/1', '2021.1.31'])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "(Timestamp('2021-01-10 00:00:00'), Timestamp('2021-10-01 00:00:00'))"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对于欧洲时区普遍采用的书写样式，我们还可以通过dayfirst=True参数修正时间输出样式\n",
    "pd.to_datetime('1-10-2021'),pd.to_datetime('1-10-2021', dayfirst=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "0   2021-01-01\n1   2021-01-02\n2   2021-01-03\ndtype: datetime64[ns]"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# to_datetime方法还能够转换Pandas中的基础数据格式Seris和DataFrame\n",
    "pd.to_datetime(pd.Series(['2021-1-1', '2021-1-2', '2021-1-3']))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2021-01-01', '2021-01-02', '2021-01-03'], dtype='datetime64[ns]', freq=None)"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(['2021-1-1','2021-1-2','2021-1-3'])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "0   2021-01-03 05:00:00\n1   2021-02-04 06:00:00\ndtype: datetime64[ns]"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(pd.DataFrame({'year': [2021, 2021], 'month': [1, 2], 'day': [3, 4], 'hour': [5, 6]}))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "ename": "ParserError",
     "evalue": "Unknown string format: invalid",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\arrays\\datetimes.py\u001B[0m in \u001B[0;36mobjects_to_datetime64ns\u001B[1;34m(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)\u001B[0m\n\u001B[0;32m   1855\u001B[0m         \u001B[1;32mtry\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m-> 1856\u001B[1;33m             \u001B[0mvalues\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mtz_parsed\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mconversion\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mdatetime_to_datetime64\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mdata\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m   1857\u001B[0m             \u001B[1;31m# If tzaware, these values represent unix timestamps, so we\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mpandas\\_libs\\tslibs\\conversion.pyx\u001B[0m in \u001B[0;36mpandas._libs.tslibs.conversion.datetime_to_datetime64\u001B[1;34m()\u001B[0m\n",
      "\u001B[1;31mTypeError\u001B[0m: Unrecognized value type: <class 'str'>",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001B[1;31mParserError\u001B[0m                               Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-28-73da43b45db1>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[1;32m----> 1\u001B[1;33m \u001B[0mpd\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mto_datetime\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;34m'2017-1-1'\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m'invalid'\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0merrors\u001B[0m\u001B[1;33m=\u001B[0m\u001B[1;34m'raise'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m      2\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\tools\\datetimes.py\u001B[0m in \u001B[0;36mto_datetime\u001B[1;34m(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)\u001B[0m\n\u001B[0;32m    752\u001B[0m             \u001B[0mresult\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0m_convert_and_box_cache\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0marg\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mcache_array\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    753\u001B[0m         \u001B[1;32melse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 754\u001B[1;33m             \u001B[0mresult\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mconvert_listlike\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0marg\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mformat\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    755\u001B[0m     \u001B[1;32melse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    756\u001B[0m         \u001B[0mresult\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mconvert_listlike\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mnp\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0marray\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m[\u001B[0m\u001B[0marg\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mformat\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;36m0\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\tools\\datetimes.py\u001B[0m in \u001B[0;36m_convert_listlike_datetimes\u001B[1;34m(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)\u001B[0m\n\u001B[0;32m    445\u001B[0m             \u001B[0merrors\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0merrors\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    446\u001B[0m             \u001B[0mrequire_iso8601\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mrequire_iso8601\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 447\u001B[1;33m             \u001B[0mallow_object\u001B[0m\u001B[1;33m=\u001B[0m\u001B[1;32mTrue\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    448\u001B[0m         )\n\u001B[0;32m    449\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\arrays\\datetimes.py\u001B[0m in \u001B[0;36mobjects_to_datetime64ns\u001B[1;34m(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)\u001B[0m\n\u001B[0;32m   1859\u001B[0m             \u001B[1;32mreturn\u001B[0m \u001B[0mvalues\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mview\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"i8\"\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mtz_parsed\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   1860\u001B[0m         \u001B[1;32mexcept\u001B[0m \u001B[1;33m(\u001B[0m\u001B[0mValueError\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mTypeError\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m-> 1861\u001B[1;33m             \u001B[1;32mraise\u001B[0m \u001B[0me\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m   1862\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   1863\u001B[0m     \u001B[1;32mif\u001B[0m \u001B[0mtz_parsed\u001B[0m \u001B[1;32mis\u001B[0m \u001B[1;32mnot\u001B[0m \u001B[1;32mNone\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\arrays\\datetimes.py\u001B[0m in \u001B[0;36mobjects_to_datetime64ns\u001B[1;34m(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)\u001B[0m\n\u001B[0;32m   1850\u001B[0m             \u001B[0mdayfirst\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mdayfirst\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   1851\u001B[0m             \u001B[0myearfirst\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0myearfirst\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m-> 1852\u001B[1;33m             \u001B[0mrequire_iso8601\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mrequire_iso8601\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m   1853\u001B[0m         )\n\u001B[0;32m   1854\u001B[0m     \u001B[1;32mexcept\u001B[0m \u001B[0mValueError\u001B[0m \u001B[1;32mas\u001B[0m \u001B[0me\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mpandas\\_libs\\tslib.pyx\u001B[0m in \u001B[0;36mpandas._libs.tslib.array_to_datetime\u001B[1;34m()\u001B[0m\n",
      "\u001B[1;32mpandas\\_libs\\tslib.pyx\u001B[0m in \u001B[0;36mpandas._libs.tslib.array_to_datetime\u001B[1;34m()\u001B[0m\n",
      "\u001B[1;32mpandas\\_libs\\tslib.pyx\u001B[0m in \u001B[0;36mpandas._libs.tslib.array_to_datetime_object\u001B[1;34m()\u001B[0m\n",
      "\u001B[1;32mpandas\\_libs\\tslib.pyx\u001B[0m in \u001B[0;36mpandas._libs.tslib.array_to_datetime_object\u001B[1;34m()\u001B[0m\n",
      "\u001B[1;32mpandas\\_libs\\tslibs\\parsing.pyx\u001B[0m in \u001B[0;36mpandas._libs.tslibs.parsing.parse_datetime_string\u001B[1;34m()\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\dateutil\\parser\\_parser.py\u001B[0m in \u001B[0;36mparse\u001B[1;34m(timestr, parserinfo, **kwargs)\u001B[0m\n\u001B[0;32m   1372\u001B[0m         \u001B[1;32mreturn\u001B[0m \u001B[0mparser\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mparserinfo\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mparse\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mtimestr\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;33m**\u001B[0m\u001B[0mkwargs\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   1373\u001B[0m     \u001B[1;32melse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m-> 1374\u001B[1;33m         \u001B[1;32mreturn\u001B[0m \u001B[0mDEFAULTPARSER\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mparse\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mtimestr\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;33m**\u001B[0m\u001B[0mkwargs\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m   1375\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   1376\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\dateutil\\parser\\_parser.py\u001B[0m in \u001B[0;36mparse\u001B[1;34m(self, timestr, default, ignoretz, tzinfos, **kwargs)\u001B[0m\n\u001B[0;32m    647\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    648\u001B[0m         \u001B[1;32mif\u001B[0m \u001B[0mres\u001B[0m \u001B[1;32mis\u001B[0m \u001B[1;32mNone\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 649\u001B[1;33m             \u001B[1;32mraise\u001B[0m \u001B[0mParserError\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"Unknown string format: %s\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mtimestr\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    650\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    651\u001B[0m         \u001B[1;32mif\u001B[0m \u001B[0mlen\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mres\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;33m==\u001B[0m \u001B[1;36m0\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mParserError\u001B[0m: Unknown string format: invalid"
     ]
    }
   ],
   "source": [
    "pd.to_datetime(['2017-1-1', 'invalid'], errors='raise')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['2017-1-1', 'invalid'], dtype='object')"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(['2017-1-1', 'invalid'], errors='ignore')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2017-01-01', 'NaT'], dtype='datetime64[ns]', freq=None)"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "pd.to_datetime(['2017-1-1', 'invalid'], errors='coerce')    # coerce 强制、迫使"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "pd.date_range()\n",
    "\"\"\"\n",
    "def date_range(start: Any = None,\n",
    "               end: Any = None,\n",
    "               periods: Any = None,\n",
    "               freq: Any = None,\n",
    "               tz: Any = None,\n",
    "               normalize: Any = False,\n",
    "               name: Any = None,\n",
    "               closed: Any = None,\n",
    "               **kwargs: Any) -> DatetimeIndex\n",
    "`start=` ：设置起始时间\n",
    "`end= `：设置结束时间\n",
    "`periods=` ：设置时间区间，若 `None` 则需要设置单独设置起止和结束时间。\n",
    "`freq=` ：设置间隔周期。\n",
    "`tz= `：设置时区。\n",
    "其中，`freq=` 设置周期：\n",
    " `freq='s' `: 秒\n",
    " `freq='min'` : 分钟\n",
    " `freq='H' `: 小时\n",
    " `freq='D' `: 天\n",
    " `freq='w' `: 周\n",
    " `freq='m' `: 月\n",
    " `freq='BM' `: 每个月最后一天\n",
    " `freq='W' `：每周的星期日\n",
    " 注意：\n",
    " 在start 、 end 、 periods和freq四个参数中，必须准确指定三个。 如果freq被省略，所得到的DatetimeIndex将具有periods线性间隔之间的元素start和end （在两侧上关闭）\n",
    "\"\"\""
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2021-01-01 00:00:00', '2021-01-01 01:00:00',\n               '2021-01-01 02:00:00', '2021-01-01 03:00:00',\n               '2021-01-01 04:00:00', '2021-01-01 05:00:00',\n               '2021-01-01 06:00:00', '2021-01-01 07:00:00',\n               '2021-01-01 08:00:00', '2021-01-01 09:00:00',\n               '2021-01-01 10:00:00', '2021-01-01 11:00:00',\n               '2021-01-01 12:00:00', '2021-01-01 13:00:00',\n               '2021-01-01 14:00:00', '2021-01-01 15:00:00',\n               '2021-01-01 16:00:00', '2021-01-01 17:00:00',\n               '2021-01-01 18:00:00', '2021-01-01 19:00:00',\n               '2021-01-01 20:00:00', '2021-01-01 21:00:00',\n               '2021-01-01 22:00:00', '2021-01-01 23:00:00',\n               '2021-01-02 00:00:00'],\n              dtype='datetime64[ns]', freq='H')"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照小时来增加序列\n",
    "pd.date_range('2021-1-1','2021-1-2',freq='H')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2021-01-01 00:00:00', '2021-01-01 00:01:00',\n               '2021-01-01 00:02:00', '2021-01-01 00:03:00',\n               '2021-01-01 00:04:00', '2021-01-01 00:05:00',\n               '2021-01-01 00:06:00', '2021-01-01 00:07:00',\n               '2021-01-01 00:08:00', '2021-01-01 00:09:00',\n               '2021-01-01 00:10:00', '2021-01-01 00:11:00',\n               '2021-01-01 00:12:00', '2021-01-01 00:13:00',\n               '2021-01-01 00:14:00'],\n              dtype='datetime64[ns]', freq='T')"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照min分钟来进行序列增加，增加periods个\n",
    "pd.date_range('2021-1-1',periods=15,freq='min')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2021-01-01 00:00:00', '2021-01-01 01:30:00',\n               '2021-01-01 03:00:00', '2021-01-01 04:30:00',\n               '2021-01-01 06:00:00', '2021-01-01 07:30:00',\n               '2021-01-01 09:00:00', '2021-01-01 10:30:00',\n               '2021-01-01 12:00:00', '2021-01-01 13:30:00',\n               '2021-01-01 15:00:00', '2021-01-01 16:30:00',\n               '2021-01-01 18:00:00', '2021-01-01 19:30:00',\n               '2021-01-01 21:00:00', '2021-01-01 22:30:00',\n               '2021-01-02 00:00:00', '2021-01-02 01:30:00',\n               '2021-01-02 03:00:00', '2021-01-02 04:30:00'],\n              dtype='datetime64[ns]', freq='90T')"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定时间段进行增加\n",
    "pd.date_range('1/1/2021', periods=20, freq='1H30min')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2021-01-02 00:00:00', '2021-01-02 01:30:00'], dtype='datetime64[ns]', freq='90T')"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间段切片\n",
    "pd.date_range('1/2/2021', periods=20, freq='1H30min')[:2]\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [],
   "source": [
    "from pandas import offsets"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2021-01-01 00:00:00', '2021-01-02 01:01:00',\n               '2021-01-03 02:02:00', '2021-01-04 03:03:00',\n               '2021-01-05 04:04:00', '2021-01-06 05:05:00',\n               '2021-01-07 06:06:00', '2021-01-08 07:07:00',\n               '2021-01-09 08:08:00', '2021-01-10 09:09:00'],\n              dtype='datetime64[ns]', freq='1501T')"
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1D1H1min 一天一小时一分\n",
    "Ti = pd.date_range('2021-1-1',periods=10,freq='1D1H1min')\n",
    "Ti\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2021-02-03 03:00:00', '2021-02-04 03:01:00',\n               '2021-02-05 03:02:00', '2021-02-06 03:03:00',\n               '2021-02-07 03:04:00', '2021-02-08 03:05:00',\n               '2021-02-09 03:06:00', '2021-02-10 03:07:00',\n               '2021-02-11 03:08:00', '2021-02-12 03:09:00'],\n              dtype='datetime64[ns]', freq='1441T')"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在Ti的基础上进行偏移\n",
    "Ti + offsets.DateOffset(months=1, days=2, hour=3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "data": {
      "text/plain": "DatetimeIndex(['2021-01-15 00:00:00', '2021-01-16 01:01:00',\n               '2021-01-17 02:02:00', '2021-01-18 03:03:00',\n               '2021-01-19 04:04:00', '2021-01-20 05:05:00',\n               '2021-01-21 06:06:00', '2021-01-22 07:07:00',\n               '2021-01-23 08:08:00', '2021-01-24 09:09:00'],\n              dtype='datetime64[ns]', freq='1501T')"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 整体偏移两周\n",
    "Ti + 2*offsets.Week()\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "data": {
      "text/plain": "Period('2021', 'A-DEC')"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "value : Period or str, default None\n",
    "            The time period represented (e.g., '4Q2005').\n",
    "        freq : str, default None\n",
    "            One of pandas period strings or corresponding objects.\n",
    "        ordinal : int, default None\n",
    "            The period offset from the gregorian proleptic epoch.\n",
    "        year : int, default None\n",
    "            Year value of the period.\n",
    "        month : int, default 1\n",
    "            Month value of the period.\n",
    "        quarter : int, default None\n",
    "            Quarter value of the period.\n",
    "        day : int, default 1\n",
    "            Day value of the period.\n",
    "        hour : int, default 0\n",
    "            Hour value of the period.\n",
    "        minute : int, default 0\n",
    "            Minute value of the period.\n",
    "        second : int, default 0\n",
    "            Second value of the period.\n",
    "value : Period 或 str，默认 None\n",
    "表示的时间段（例如，'4Q2005'）。\n",
    "频率： str，默认无\n",
    "熊猫时期字符串或相应对象之一。\n",
    "序数： int，默认无\n",
    "该时期偏离了格里高利催眠时期。\n",
    "年份：整数，默认无\n",
    "期间的年份值。\n",
    "月份：整数，默认为 1\n",
    "期间的月份值。\n",
    "季度：整数，默认无\n",
    "期间的季度值。\n",
    "天：整数，默认 1\n",
    "期间的天值。\n",
    "小时：整数，默认 0\n",
    "期间的小时值。\n",
    "分钟：整数，默认为 0\n",
    "期间的分钟值。\n",
    "第二个：整数，默认 0\n",
    "期间的第二个值\n",
    "\"\"\"\n",
    "pd.Period('2021')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "data": {
      "text/plain": "(Period('2021-01', 'M'),\n Period('2021-01-01', 'D'),\n Period('2021-01-01 12:00', 'H'),\n Period('2021-01-01 12:00', 'T'),\n Period('2021-01-01 12:05:00', 'S'))"
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 代表一段时间\n",
    "pd.Period('2021-1'),pd.Period('2021-1-1'),pd.Period('2021-1-1 12'),pd.Period('2021-1-1 12:00'),pd.Period('2021-1-1 12:05:00')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "data": {
      "text/plain": "PeriodIndex(['2021-01', '2021-02', '2021-03', '2021-04', '2021-05', '2021-06',\n             '2021-07', '2021-08', '2021-09', '2021-10', '2021-11', '2021-12',\n             '2022-01'],\n            dtype='period[M]', freq='M')"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.period_range('2021-1','2022-1',freq='M')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [],
   "source": [
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-01-01   -0.603115\n2021-01-02    0.872461\n2021-01-03   -0.762707\n2021-01-04    0.471661\n2021-01-05   -1.492217\n2021-01-06   -0.198900\n2021-01-07   -0.946777\n2021-01-08   -0.912324\n2021-01-09   -2.178122\n2021-01-10    0.480641\nFreq: D, dtype: float64"
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Ti = pd.date_range('2021-1-1', periods=10, freq='D')\n",
    "data = pd.Series(np.random.randn(len(Ti)), index = Ti)\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "outputs": [
    {
     "data": {
      "text/plain": "-0.6031151527826335"
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['2021-1-1']"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-01-01   -0.603115\n2021-01-02    0.872461\n2021-01-03   -0.762707\n2021-01-04    0.471661\n2021-01-05   -1.492217\n2021-01-06   -0.198900\nFreq: D, dtype: float64"
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['2021-1-1':'2021-1-6']"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-01-01   -0.603115\n2021-01-02    0.872461\n2021-01-03   -0.762707\n2021-01-04    0.471661\n2021-01-05   -1.492217\n2021-01-06   -0.198900\nFreq: D, dtype: float64"
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "def truncate(self: FrameOrSeries,\n",
    "             before: Any = None,\n",
    "             after: Any = None,\n",
    "             axis: Any = None,\n",
    "             copy: bool = True) -> FrameOrSeries\n",
    "\"\"\"\n",
    "data.truncate(before='2021-1-1',after='2021-1-6')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-05-01   -1.220576\n2021-05-02    0.533060\n2021-05-03    0.050839\n2021-05-04   -0.791398\n2021-05-05   -0.340513\n2021-05-06    0.966905\n2021-05-07   -2.721515\n2021-05-08    2.723596\n2021-05-09   -0.261622\n2021-05-10    0.895435\n2021-05-11    0.126575\n2021-05-12    0.157193\n2021-05-13   -0.285388\n2021-05-14    0.955425\n2021-05-15    1.672253\n2021-05-16   -1.173588\n2021-05-17    1.479170\n2021-05-18   -1.299394\n2021-05-19    0.763252\n2021-05-20    0.013718\nFreq: D, dtype: float64"
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Ti = pd.date_range('2021-5-1', periods=20, freq='D')\n",
    "data = pd.Series(np.random.randn(len(Ti)), index = Ti)\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-05-01   -1.768589\n2021-05-06    1.602799\n2021-05-11    2.626058\n2021-05-16   -0.216841\nFreq: 5D, dtype: float64"
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 相当于窗口函数\n",
    "data.resample('5D').sum()\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-05-01   -0.353718\n2021-05-06    0.320560\n2021-05-11    0.525212\n2021-05-16   -0.043368\nFreq: 5D, dtype: float64"
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.resample('5D').mean()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-05-01    0.533060\n2021-05-06    2.723596\n2021-05-11    1.672253\n2021-05-16    1.479170\nFreq: 5D, dtype: float64"
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.resample('5D').max()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "outputs": [
    {
     "data": {
      "text/plain": "                open      high       low     close\n2021-05-01 -1.220576  0.533060 -1.220576 -0.340513\n2021-05-06  0.966905  2.723596 -2.721515  0.895435\n2021-05-11  0.126575  1.672253 -0.285388  1.672253\n2021-05-16 -1.173588  1.479170 -1.299394  0.013718",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>open</th>\n      <th>high</th>\n      <th>low</th>\n      <th>close</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2021-05-01</th>\n      <td>-1.220576</td>\n      <td>0.533060</td>\n      <td>-1.220576</td>\n      <td>-0.340513</td>\n    </tr>\n    <tr>\n      <th>2021-05-06</th>\n      <td>0.966905</td>\n      <td>2.723596</td>\n      <td>-2.721515</td>\n      <td>0.895435</td>\n    </tr>\n    <tr>\n      <th>2021-05-11</th>\n      <td>0.126575</td>\n      <td>1.672253</td>\n      <td>-0.285388</td>\n      <td>1.672253</td>\n    </tr>\n    <tr>\n      <th>2021-05-16</th>\n      <td>-1.173588</td>\n      <td>1.479170</td>\n      <td>-1.299394</td>\n      <td>0.013718</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ohlc 并列出对应 5天数据的原值（open）、最大值（high）、最小值（low）、以及临近值（close）\n",
    "data.resample('5D').ohlc()\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-05-01 00:00:00   -1.220576\n2021-05-01 01:00:00   -1.220576\n2021-05-01 02:00:00   -1.220576\n2021-05-01 03:00:00   -1.220576\n2021-05-01 04:00:00   -1.220576\n                         ...   \n2021-05-19 20:00:00    0.763252\n2021-05-19 21:00:00    0.763252\n2021-05-19 22:00:00    0.763252\n2021-05-19 23:00:00    0.763252\n2021-05-20 00:00:00    0.013718\nFreq: H, Length: 457, dtype: float64"
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# resample方法还可以用来升频采样；如下，利用resample方法将时间频率提升到小时，并利用ffill方法对新增加的行用与原始数据相同的数据进行填充\n",
    "data.resample('H').ffill()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-05-01 00:00:00   -1.220576\n2021-05-01 01:00:00         NaN\n2021-05-01 02:00:00         NaN\n2021-05-01 03:00:00         NaN\n2021-05-01 04:00:00         NaN\n                         ...   \n2021-05-19 20:00:00         NaN\n2021-05-19 21:00:00         NaN\n2021-05-19 22:00:00         NaN\n2021-05-19 23:00:00         NaN\n2021-05-20 00:00:00    0.013718\nFreq: H, Length: 457, dtype: float64"
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也可以利用asfreq方法对新增加的行不进行填充\n",
    "data.resample('H').asfreq()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-05-01 00:00:00   -1.220576\n2021-05-01 01:00:00   -1.220576\n2021-05-01 02:00:00   -1.220576\n2021-05-01 03:00:00   -1.220576\n2021-05-01 04:00:00         NaN\n                         ...   \n2021-05-19 20:00:00         NaN\n2021-05-19 21:00:00         NaN\n2021-05-19 22:00:00         NaN\n2021-05-19 23:00:00         NaN\n2021-05-20 00:00:00    0.013718\nFreq: H, Length: 457, dtype: float64"
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间频率从天提升到小时，只对每个间隔新增加的前 3 行填充\n",
    "data.resample('H').ffill(limit=3)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-05-01         NaN\n2021-05-02         NaN\n2021-05-03         NaN\n2021-05-04   -1.220576\n2021-05-05    0.533060\n2021-05-06    0.050839\n2021-05-07   -0.791398\n2021-05-08   -0.340513\n2021-05-09    0.966905\n2021-05-10   -2.721515\n2021-05-11    2.723596\n2021-05-12   -0.261622\n2021-05-13    0.895435\n2021-05-14    0.126575\n2021-05-15    0.157193\n2021-05-16   -0.285388\n2021-05-17    0.955425\n2021-05-18    1.672253\n2021-05-19   -1.173588\n2021-05-20    1.479170\nFreq: D, dtype: float64"
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shift(3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-05-01   -0.791398\n2021-05-02   -0.340513\n2021-05-03    0.966905\n2021-05-04   -2.721515\n2021-05-05    2.723596\n2021-05-06   -0.261622\n2021-05-07    0.895435\n2021-05-08    0.126575\n2021-05-09    0.157193\n2021-05-10   -0.285388\n2021-05-11    0.955425\n2021-05-12    1.672253\n2021-05-13   -1.173588\n2021-05-14    1.479170\n2021-05-15   -1.299394\n2021-05-16    0.763252\n2021-05-17    0.013718\n2021-05-18         NaN\n2021-05-19         NaN\n2021-05-20         NaN\nFreq: D, dtype: float64"
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shift(-3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "outputs": [
    {
     "data": {
      "text/plain": "2021-05-04   -1.220576\n2021-05-05    0.533060\n2021-05-06    0.050839\n2021-05-07   -0.791398\n2021-05-08   -0.340513\n2021-05-09    0.966905\n2021-05-10   -2.721515\n2021-05-11    2.723596\n2021-05-12   -0.261622\n2021-05-13    0.895435\n2021-05-14    0.126575\n2021-05-15    0.157193\n2021-05-16   -0.285388\n2021-05-17    0.955425\n2021-05-18    1.672253\n2021-05-19   -1.173588\n2021-05-20    1.479170\n2021-05-21   -1.299394\n2021-05-22    0.763252\n2021-05-23    0.013718\nFreq: D, dtype: float64"
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shift(3,freq='D')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}